package com.rose.springbootmoredatasourcejpa.controller;

import com.rose.springbootmoredatasourcejpa.annotation.DataSource;
import com.rose.springbootmoredatasourcejpa.entity.Person;
import com.rose.springbootmoredatasourcejpa.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "person")
public class PerconController {

    @Autowired
    private PersonRepository personRepository;

    @PostMapping(path = "addPerson")
    public void addPerson(Person person) {
        personRepository.save(person);
    }

    @DeleteMapping(path = "deletePerson")
    public void deletePerson(Long id) {
        Person person = new Person();
        person.setId(id);
        personRepository.delete(person);
    }
    @GetMapping(path = "list1")
    @DataSource("slave1")
    public List<Person> list1(Long id) {
        return personRepository.findAll();
    }
    @GetMapping(path = "list2")
    @DataSource("slave2")
    public List<Person> list2(Long id) {
        return personRepository.findAll();
    }
}